<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="departments" highlight-current-row v-loading="listLoading" @row-dblclick="rowDblclick" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column label="编号" type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" cell-click="cell-click" label="数据字典名" width="360" sortable>
			</el-table-column>
			<el-table-column prop="sn" label="编号" sortable>
			</el-table-column>
			<el-table-column>
        <template slot="header" slot-scope="scope">
          <el-button size="small" type="danger" align="center" @click="handleAdd">添加</el-button>
        </template>
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="small" type="danger" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :current-page="currentPage"
                     :total="total"
                     style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
    <!--
    rules： 表单校验规则
    ref： 给表单设置一个引用别名
    -->
		<el-dialog :title="title" :visible.sync="addOrUpFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="字典名" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="字典编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addOrUpFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addOrUpSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

    <!--抽屉-->
    <el-drawer
        title="数据字典细节"
        :visible.sync="Dictionary"
        direction="rtl"
        :before-close="handleClose"
        size="50%">
      <!--工具条-->
      <el-col :span="24" class="toolbarDetail" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="filters">
          <el-form-item>
            <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="searchDetail">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleAddDetail">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-table :data="gridData" @selection-change="selsChangeDetail">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column label="编号" type="index" width="60">
        </el-table-column>
        <el-table-column property="name" label="明细名" width="200"></el-table-column>
        <el-table-column>
          <template slot="header" slot-scope="scope">
            <el-button size="small" type="danger" align="center" @click="handleAddDetail">添加</el-button>
          </template>
          <template slot-scope="scope">
            <el-button size="small" @click="handleEditDetail(scope.$index, scope.row)">修改</el-button>
            <el-button size="small" type="danger" @click="handleDelDetail(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--工具条-->
      <el-col :span="24" class="toolbarDetail">
        <el-button type="danger" @click="batchRemoveDetail" :disabled="this.selsDetail.length===0">批量删除</el-button>
        <el-pagination layout="prev, pager, next"
                       @current-change="handleCurrentChangeDetail"
                       :page-size="pageSizeDetail"
                       :current-page="currentPageDetail"
                       :total="totalDetail"
                       style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>


    <!--抽屉的编辑界面-->
    <!--
    rules： 表单校验规则
    ref： 给表单设置一个引用别名
    -->
    <div>
    <el-dialog :title="title" :visible.sync="addOrUpFormVisibleDetail" :close-on-click-modal="false">
      <el-form :model="editFormDetail" label-width="80px" :rules="editFormRulesDetail" ref="editForm">
        <el-form-item type="hide" prop="id">
        </el-form-item>
        <el-form-item label="明细名" prop="name">
          <el-input v-model="editFormDetail.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addOrUpFormVisibleDetail = false">取消</el-button>
        <el-button type="primary" @click.native="addOrUpSubmitDetail" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
    </div>

	</section>
</template>

<script>
//import NProgress from 'nprogress'

export default {
		data() {
			return {
        title:'',
				filters: {
          keyword: ''  //关键字查询
				},
        employees:[],
        Dictionary:false,//抽屉
        gridData:[],
				departments: [],
        deptTree:[],
				total: 0,
        rowDetail:null,
        totalDetail: 0,
        types_id:0,
        pageSize:5,
        currentPage: 1,
        pageSizeDetail:5,
        currentPageDetail: 1,
				listLoading: false,
				sels: [],//列表选中列
        selsDetail: [],//列表选中列
        addOrUpFormVisibleDetail:false,//抽屉的编辑界面是否显示
				addOrUpFormVisible: false,//编辑界面是否显示
				editLoading: false,//表单提交按钮是否忙等
				editFormRules: {//表单校验规则
					name: [
						{ required: true, message: '请输入产品品牌', trigger: 'blur' }
					],
          sn: [
            { required: true, message: '请输入产品单位', trigger: 'blur' }
          ]
				},
        editFormRulesDetail: {//表单校验规则
          name: [
            { required: true, message: '请输入数据明细', trigger: 'blur' }
          ]
        },
				//编辑界面数据
				editForm: {//重置表单
					id: null,
          sn: '',
          name: ''
				},
        //抽屉的编辑界面数据
        editFormDetail: {//重置表单
          id: null,
          types_id: '',
          name: ''
        },
			}
		},
    //点击弹出抽屉
		methods: {
      rowDblclick(row, column, event){
        this.Dictionary = true;//点击开启抽屉弹窗
        this.rowDetail = row.id
        console.log(this.rowDetail+"这里是")
        this.getDetails();
      },
      search(){//高级查询一定要定位到第一页
        this.currentPage = 1;
        this.getDictionary();
      },
      searchDetail(){//高级查询一定要定位到第一页
        this.currentPage = 1;
        this.getDetails();
      },
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDictionary();
			},
      handleCurrentChangeDetail(val) {
        this.currentPage = val;
        this.getDetails();
      },
			//获取主表列表
			getDictionary() {
				let para = {
				  //当前页
          currentPage: this.currentPage,
          //当前页每页展示数据条数
					pageSize: this.pageSize,
          //高级查询关键字
          keyword: this.filters.keyword
				};
				console.log(para.currentPage+"adad"+para.pageSize)
				this.listLoading = true;//开启列表忙等
        this.$http.post("/type",para)
        .then(result=>{
             this.listLoading = false;//关闭列表忙等
            result  = result.data;//PageList
            this.departments = result.rows;
            this.total = result.total;
        })
        .catch(result=>{
          //console.log(result);
          this.$message({
            message: '系统繁忙，稍后重试！',
            type: 'error'
          });
        })
			},
			//删除
			handleDel: function (index, row) {
        //console.log(row);//一行所有数据
        //console.log(index)//列表中的索引位置
				this.$confirm('确认删除该记录吗?', '温馨提示', {
					type: 'warning'
				}).then(() => {//确定
					this.listLoading = true;//开启列表加载忙等
          //url /dept/id 做拼接
					this.$http.delete("/type/"+row.id,'id')
              .then(result=>{
                this.listLoading = false;//关闭列表加载忙等
                result = result.data;
                if(!result.success){
                  this.$message({
                    message:result.message,
                    type:'error'
                  })
                }
                  this.$message({
                    message:result.message,
                    type:'success'
                  });

                this.currentPage = 1;
                this.getDictionary();
              })
              .catch(result=>{
                //console.log(result);
                this.$message({
                  message:result.message,
                  type:'error'
                })
              })
				}).catch(() => {//取消

				});
			},
      //抽屉的删除
      handleDelDetail: function (index, row) {
        console.log(row);//一行所有数据
        //console.log(index)//列表中的索引位置
        this.$confirm('确认删除该记录吗?', '温馨提示', {
          type: 'warning'
        }).then(() => {//确定
          this.listLoading = true;//开启列表加载忙等
          //url /dept/id 做拼接
          this.$http.delete("/detail/"+row.id,'id')
              .then(result=>{
                this.listLoading = false;//关闭列表加载忙等
                console.log(result)
                result = result.data;
                if(!result.success){
                  this.$message({
                    message:result.message,
                    type:'error'
                  })
                }
                this.$message({
                  message:result.message,
                  type:'success'
                });

                this.currentPage = 1;
                this.getDetails(row);
              })
              .catch(result=>{
                //console.log(result);
                this.$message({
                  message:result.message,
                  type:'error'
                })
              })
        }).catch(() => {//取消

        });
      },
			//显示编辑界面
			handleEdit: function (index, row) {
        this.title="修改数据字典";
				this.addOrUpFormVisible= true;
				//Object.assign({}, row);把当前行的数据克隆给一个新的对象
				this.editForm = Object.assign({}, row);

			},
      //显示抽屉编辑界面
      handleEditDetail: function (index, row) {
        this.title="修改明细";
        this.addOrUpFormVisibleDetail= true;
        //Object.assign({}, row);把当前行的数据克隆给一个新的对象
        this.editFormDetail = Object.assign({}, row);

      },
			//显示新增界面
      handleAdd: function () {
        this.title="添加数据字典";
        this.addOrUpFormVisible = true;//弹出表单
				this.editForm = {//重置表单
          id: null,
          sn:'',
          name: ''
				};
			},
      //显示抽屉添加页面
      handleAddDetail(){
        this.title = "添加明细";
        this.addOrUpFormVisibleDetail = true;
        this.editFormDetail = {//重置表单
          id: null,
          name: "",
          types_id: ""
        };
      },
			//编辑
			addOrUpSubmit: function () {
        //this.$refs 拿到当前表单中的所有ref
				this.$refs.editForm.validate((valid) => {//主动开启表单校验
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;//开启表单提交按钮忙等
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							/*处理后端需要的数据格式*/
              if(para.parent){
                para.parent = {"id":para.parent[para.parent.length - 1]}//parent只需要最后一个数据
              }
							/*para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');*/
								//NProgress.done();
                this.$http.put("/type",para)
                    .then(result=>{
                      this.editLoading = false;//关闭列表加载忙等
                      result = result.data;
                      if(!result.success){
                        this.$message({
                          message:result.message,
                          type:'error'
                        })
                      }else {//成功了关闭模态框
                        this.$message({
                          message: result.message,
                          type: 'success'
                        });
                        this.addOrUpFormVisible = false;//关闭模态框
                      }
                      //修改成功刷新页面
                      this.getDictionary();
                    })
                    .catch(result=>{
                      this.editLoading = false;
                      //console.log(result);
                      this.$message({
                        message:result.message,
                        type:'error'
                      })
                    })
						});
					}
				});
			},
      //抽屉编辑
      addOrUpSubmitDetail: function () {
        //this.$refs 拿到当前页面上的所有ref
        //this.$refs.editForm  找editForm
        this.$refs.editForm.validate((valid) => {//主动开启表单校验
          if (valid) {//校验没问题
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true;// 开启表单提交按钮忙等  防止你重复点击
              let para = Object.assign({}, this.editFormDetail);
              para.types_id = this.rowDetail;
              console.log(para.id)
              console.log(para.types_id+"抽屉里面");
              this.$http.put("/detail",para)
                  .then(result=>{
                    this.editLoading = false;//关闭按钮忙等
                    result = result.data;//AjaxResult
                    if(!result.success){
                      this.$message({
                        message: result.message,
                        type: 'error'
                      });
                    }else{//成功才关闭模态框
                      this.$message({
                        message: result.message,
                        type: 'success'
                      });
                      this.addOrUpFormVisibleDetail = false;//关闭模态框
                    }
                    this.getDetails();
                  })
                  .catch(result=>{
                    this.editLoading = false;
                    this.$message({
                      message: '系统繁忙，稍后重试！',
                      type: 'error'
                    });
                  })
            });
          }
        });
      },
			selsChange: function (sels) {
        //当前选中的所有行数据
				this.sels = sels;
			},
      selsChangeDetail: function (selsDetail) {
        //当前选中的所有行数据
        this.selsDetail = selsDetail;
      },
			//批量删除
			batchRemove: function () {
        //this.sels  当前选中的所有行数据
				var ids = this.sels.map(item => item.id);//[1,2,3,...]
				//console.log(ids)
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {//确定删除
					this.listLoading = true;
					//NProgress.start();
					/*let para = { ids: ids };*///如果在上面的ids加了 .toString那么就要加此行
          this.$http.patch("/type",ids)
              .then(result=>{
                this.listLoading = false;//关闭忙等
                result = result.data;
                if (result.success){//如果删除成功
                  this.$message({
                    message:result.message,
                    type:"success"
                  })
                }else{
                  this.$message({
                    message:result.message,
                    type:"error"
                  })
                }
                //数据删除要定位到第一页
                this.currentPage = 1;
                this.getDictionary();
              })
              .catch(result=>{
                //console.log(result);
                this.$message({
                  message:"系统繁忙,请稍后再试!!!",
                  type:"error"
                })
              })

				}).catch(() => {

				});
			},
      //抽屉批量删除
      batchRemoveDetail: function () {
        //this.selsDetail  当前选中的所有行数据
        var ids = this.selsDetail.map(item => item.id);//[1,2,3,...]
        //console.log(ids)
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {//确定删除
          this.listLoading = true;
          //NProgress.start();
          /*let para = { ids: ids };*///如果在上面的ids加了 .toString那么就要加此行
          this.$http.patch("/detail",ids)
              .then(result=>{
                this.listLoading = false;//关闭忙等
                result = result.data;
                if (result.success){//如果删除成功
                  this.$message({
                    message:result.message,
                    type:"success"
                  })
                }else{
                  this.$message({
                    message:result.message,
                    type:"error"
                  })
                }
                //数据删除要定位到第一页
                this.currentPageDetail = 1;
                this.getDetails();
              })
              .catch(result=>{
                //console.log(result);
                this.$message({
                  message:"系统繁忙,请稍后再试!!!",
                  type:"error"
                })
              })

        }).catch(() => {

        });
      },
      getDeptTree(){
        this.$http.get("/dept/tree")
            .then(result=>{
              result = result.data;
              if (result){
                this.gridData=result.row;
                this.totalDetail=result.total;
              }
              //console.log(this.deptTree);
            })
      },
      //抽屉中的数据
      handleClose(done) {
        this.$confirm('确认关闭？')
            .then(_ => {
              done();
            })
            .catch(_ => {});
      },
      getDetails:function (){
        let para = {
          types_id: this.rowDetail,
          currentPage: this.currentPageDetail,
          pageSize: this.pageSizeDetail,
          keyword: this.filters.keyword
        };
        console.log(para.currentPage+"da"+para.pageSize+"这里是抽屉里的数据");
        this.$http.post("/detail",para)
            .then(result=>{
            result= result.data;
            console.log(result)
            if (result){
              this.gridData = result.rows;
              this.totalDetail = result.total;
              console.log(this.totalDetail)
            }
            console.log()
        }).catch(result=>{

        })
      }
		},
		mounted() {
			this.getDictionary();
		}
	}

</script>

<style scoped>

</style>